package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.Compent;
import com.oracle.common.PageResult;
import com.oracle.common.TaskStatusEnum;
import com.oracle.mapper.OauserMapper;
import com.oracle.mapper.TaskMapper;
import com.oracle.pojo.Oauser;
import com.oracle.pojo.OauserExample;
import com.oracle.pojo.Task;
import com.oracle.pojo.TaskExample;
import com.oracle.service.TaskService;
import com.oracle.util.DateUtil;
import com.oracle.util.TaskExcelUtil;
import com.oracle.vo.TaskVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private OauserMapper oauserMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Override
    public List<Task> findList() {
        List<Task> tasks = taskMapper.selectByExample(null);
        return tasks;
    }

    @Override
    public PageResult findPages(Integer pageNo, Integer pageSize, TaskVo taskVo) {
        //1、在查询数据库之前，在插件中封装页码
        PageHelper.startPage(pageNo,pageSize);

        //2、拼接查询
        TaskExample example = new TaskExample();
        TaskExample.Criteria criteria = example.createCriteria();

        if(!ObjectUtils.isEmpty(taskVo.getStartTime())){
            Date startDate = DateUtil.getDate(taskVo.getStartTime());
            criteria.andLauchtimeGreaterThan(startDate);
        }

        if(!ObjectUtils.isEmpty(taskVo.getEndTime())){
            Date endDate = DateUtil.getDate(taskVo.getEndTime());
            criteria.andLauchtimeLessThan(endDate);
        }

        if(!ObjectUtils.isEmpty(taskVo.getTypeid())){
            criteria.andTypeidEqualTo(taskVo.getTypeid());
        }

        if(!ObjectUtils.isEmpty(taskVo.getState())){
            criteria.andStateEqualTo(taskVo.getState());
        }

        criteria.andIsdeleteEqualTo(Compent.not_delete);

        example.setOrderByClause("lauchtime");

        //2、执行查询
        Page<Task> taskPage = (Page<Task>)taskMapper.selectByExample(example);

        //3、数据优化
        List<Task> taskList = taskPage.getResult();

        List<TaskVo> taskVoList = buildTaskVoList(taskList);

        return new PageResult(taskPage.getTotal(),taskVoList);
    }

    private List<TaskVo> buildTaskVoList(List<Task> taskList){
        // 视图对象容器
        List<TaskVo> taskVoList = new ArrayList<TaskVo>();
        //遍历
        taskList.forEach(task -> {
            TaskVo taskVo2 = new TaskVo();

            //将task中的同名属性赋值到taskVo2
            BeanUtils.copyProperties(task,taskVo2);

            //申请时间
            String lauchtime = DateUtil.getString(task.getLauchtime());
            taskVo2.setStartTime(lauchtime);

            //审批时间
            String subtime = DateUtil.getString(task.getSubtime());
            taskVo2.setEndTime(subtime);

            //获取待处理任务发起人的真实名 没有对数据库sql 投影做优化
            Oauser oauser = oauserMapper.selectByPrimaryKey(task.getUserid());
            taskVo2.setTrueName(oauser.getTruename());

            //封装状态  增强系统健壮性
            if(!ObjectUtils.isEmpty(task) && !ObjectUtils.isEmpty(task.getState())){
                if(0 == task.getState()){
                    taskVo2.setTaskStatus(TaskStatusEnum.PENDING.getStatus_msg());
                }else if(1 == task.getState()){
                    taskVo2.setTaskStatus(TaskStatusEnum.APPROVED.getStatus_msg());
                }else if(-1 == task.getState()){
                    taskVo2.setTaskStatus(TaskStatusEnum.REJECT.getStatus_msg());
                }
            }

            taskVoList.add(taskVo2);

        });

        return taskVoList;
    }

    @Override
    public void exportTaskList(Integer[] ids) {
        //1、从mysql中抽取数据
        TaskExample example = new TaskExample();
        TaskExample.Criteria criteria = example.createCriteria();
        //Arrays 操作数组
//        Collections 操作容器
        criteria.andIdIn(Arrays.asList(ids));
        List<Task> taskList = taskMapper.selectByExample(example);

        List<TaskVo> taskVoList = buildTaskVoList(taskList);

        //2、通过工具类生成excel
        TaskExcelUtil excelUtil = new TaskExcelUtil();
        try {
            excelUtil.exportExcel(request,response,"pendingTaskList",taskVoList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



